<template>
    <div>
        <!-- <el-card class="!border-none mb-4" shadow="never">
            <el-form
                class="mb-[-16px]"
                :model="queryParams"
                inline
            >
                <el-form-item label="墓地名称" prop="name">
                    <el-input class="w-[280px]" v-model="queryParams.name" clearable placeholder="请输入墓地名称" />
                </el-form-item>
                <el-form-item label="创建人" prop="creator">
                    <el-input class="w-[200px]" v-model="queryParams.creator" clearable placeholder="请输入创建人" />
                </el-form-item>
                <el-form-item label="审核状态" prop="audit_status">
                    <el-select v-model="queryParams.audit_status" placeholder="请选择审核状态" clearable class="w-[150px]">
                        <el-option label="全部" value="" />
                        <el-option label="待审核" :value="0" />
                        <el-option label="审核通过" :value="1" />
                        <el-option label="审核拒绝" :value="2" />
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card> -->
        <el-card class="!border-none" v-loading="pager.loading" shadow="never">
            <div class="mt-4">
                <el-table :data="pager.lists" @selection-change="handleSelectionChange">
                    <el-table-column label="逝者姓名" prop="name" show-overflow-tooltip min-width="200"/>
                    <el-table-column label="逝者照片" prop="avatar" show-overflow-tooltip min-width="120">
                        <template #default="{ row }">
                            <el-image :src="row.avatar" fit="cover" style="width: 80px;height: 80px;"/>
                        </template>
                    </el-table-column>
                    <el-table-column label="墓地别名" prop="alias" show-overflow-tooltip min-width="150"/>
                    <el-table-column label="创建人" prop="user_id" show-overflow-tooltip min-width="150"/>
                    <el-table-column label="审核状态" prop="is_disable" show-overflow-tooltip min-width="120">
                        <template #default="{ row }">
                            <el-tag 
                                :type="getAuditStatusType(row.is_disable)"
                                effect="light"
                            >
                                {{ getAuditStatusText(row.is_disable) }}
                            </el-tag>
                        </template>
                    </el-table-column>
                    <!-- <el-table-column label="创建时间" prop="create_time" show-overflow-tooltip min-width="160">
                        <template #default="{ row }">
                            {{ timeFormat(row.create_time, 'yyyy-mm-dd hh:MM:ss') }}
                        </template>
                    </el-table-column> -->
                    <el-table-column label="操作" width="120" fixed="right">
                        <template #default="{ row }">
                            <el-button 
                                v-if="row.is_disable == 0" 
                                type="primary" 
                                link 
                                @click="handleAudit(row)"
                            >
                                审核
                            </el-button>
                            <el-button 
                                v-else
                                type="info" 
                                link 
                                @click="handleViewAudit(row)"
                            >
                                查看审核
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div class="flex mt-4 justify-end">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <audit-popup v-if="showAudit" ref="auditRef" @success="getLists" @close="showAudit = false" />
    </div>
</template>

<script lang="ts" setup name="graveyardAudit">
import { usePaging } from '@/hooks/usePaging'
import { apiGraveyardLists } from '@/api/graveyardManage/list'
import { timeFormat } from '@/utils/util'
import AuditPopup from './edit.vue'
import { reactive, ref, shallowRef, nextTick, onMounted } from 'vue'

const auditRef = shallowRef<InstanceType<typeof AuditPopup>>()
// 是否显示审核弹窗
const showAudit = ref(false)

// 查询条件
const queryParams = reactive({
    // name: '',
})

// 选中数据
const selectData = ref<any[]>([])

// 表格选择后回调事件
const handleSelectionChange = (val: any[]) => {
    selectData.value = val.map(({ id }) => id)
}

// 分页相关
const { pager, getLists, resetParams, resetPage } = usePaging({
    fetchFun: apiGraveyardLists,
    params: queryParams
})

// 获取审核状态文本
const getAuditStatusText = (status: number) => {
    const statusMap: Record<number, string> = {
        0: '待审核',
        1: '审核通过',
        2: '审核拒绝'
    }
    return statusMap[status] || '未知状态'
}

// 获取审核状态标签类型
const getAuditStatusType = (status: number) => {
    const typeMap: Record<number, string> = {
        0: 'warning',
        1: 'success',
        2: 'danger'
    }
    return typeMap[status] || 'info'
}

// 审核操作
const handleAudit = async (data: any) => {
    showAudit.value = true
    await nextTick()
    auditRef.value?.open('audit')
    auditRef.value?.setFormData(data)
}

// 查看审核结果
const handleViewAudit = async (data: any) => {
    showAudit.value = true
    await nextTick()
    auditRef.value?.open('view')
    auditRef.value?.setFormData(data)
}

onMounted(() => {
    getLists()
})
</script>

<style scoped>
</style>